﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.UICommon;
using ESOL.WinUI.Hr;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraEditors.Controls;

namespace ESOL.WinUI.Finance
{
    public partial class frmMaintainPrepaidAmount : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Variable
        private MstPersonEntity mSelectedPerson;
        private FinAdvanceAmountEntity mSelectedAdvanceAmount;
        public ApproveType ThisApproveType { get; set; }
        #endregion

        #region Constructor
        public frmMaintainPrepaidAmount(FinAdvanceAmountEntity entity)
        {
            InitializeComponent();
            mSelectedAdvanceAmount = entity;
        }
        public frmMaintainPrepaidAmount(MstPersonEntity person)
        {
            InitializeComponent();
            mSelectedPerson = person;
        }
        public frmMaintainPrepaidAmount()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void SetPersonFields()
        {
            txtPerson.Text = "";
            txtPersonNameCn.Text = "";
            cmbStatus.SelectedIndex = -1;
            txtLeftAdvanceAmount.Text = "";
            if (mSelectedPerson == null)
            {
                return;
            }
            txtPerson.Text = mSelectedPerson.PersonCode;
            txtPersonNameCn.Text = mSelectedPerson.NameCn;
            cmbStatus.SetSelectedValue(mSelectedPerson.Status);
            txtLeftAdvanceAmount.Text = mSelectedPerson.AdvanceAmount.ToString("#0.00");
        }
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "AMS", false);

            DataTable dt = new DataTable();
            dt.Columns.Add("ValueMember", typeof(string));
            dt.Columns.Add("DisplayMember", typeof(string));
            Array array = Enum.GetValues(typeof(PaymentType));
            foreach (Enum enumValue in array)
            {
                DataRow dr = dt.NewRow();
                dr["DisplayMember"] = enumValue.ToString();
                dr["ValueMember"] = Convert.ToInt32(enumValue).ToString();
                dt.Rows.Add(dr);
            }
            dt.AcceptChanges();
            treePaymentType.DisplayMember = "DisplayMember";
            treePaymentType.ValueMember = "ValueMember";
            treePaymentType.DataSource = dt;
        }
        private void SetFields()
        {
            if (mSelectedAdvanceAmount == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(mSelectedAdvanceAmount.PersonId) == false)
            {
                mSelectedPerson = MstPersonBs.GetMstPersonByID(mSelectedAdvanceAmount.PersonId);
                SetPersonFields();
            }
            if (mSelectedAdvanceAmount.AdvanceDate.ToString("yyyy-MM-dd") == "1900-01-01" ||
                 mSelectedAdvanceAmount.AdvanceDate.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                dtpAdvanceDate.EditValue = null;
            }
            else
            {
                dtpAdvanceDate.EditValue = mSelectedAdvanceAmount.AdvanceDate;
            }
            cmbStatus.SetSelectedValue(mSelectedAdvanceAmount.Status);
            txtAdvanceAmount.Text = mSelectedAdvanceAmount.Amount.ToString("#0.00");
            txtRemark.Text = mSelectedAdvanceAmount.Remark;

            if (string.IsNullOrEmpty(mSelectedAdvanceAmount.PaymentType) == false)
            {
                foreach (CheckedListBoxItem node in treePaymentType.Items)
                {
                    if (mSelectedAdvanceAmount.PaymentType.IndexOf(node.Value.ToString()) >= 0)
                    {
                        node.CheckState = CheckState.Checked;
                    }
                }
            }

            UICommonFunctions.SetTextBoxDate(txtCreatedDate, mSelectedAdvanceAmount.CreatedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtCreatedBy, mSelectedAdvanceAmount.CreatedBy);
            UICommonFunctions.SetTextBoxDate(txtLastModifiedDate, mSelectedAdvanceAmount.LastModifiedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtLastModifiedBy, mSelectedAdvanceAmount.LastModifiedBy);
            UICommonFunctions.SetTextBoxDate(txtApprovedDate, mSelectedAdvanceAmount.ApprovedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtApprovedBy, mSelectedAdvanceAmount.ApprovedBy);
        }
        private void SetEntity()
        {
            if (mSelectedAdvanceAmount == null)
            {
                mSelectedAdvanceAmount = new FinAdvanceAmountEntity();
                mSelectedAdvanceAmount.AdvanceId = Guid.NewGuid().ToString();
            }
            if (mSelectedPerson != null)
            {
                mSelectedAdvanceAmount.PersonId = mSelectedPerson.PersonId;
            }
            if (dtpAdvanceDate.EditValue!=null)
            {
                mSelectedAdvanceAmount.AdvanceDate = Convert.ToDateTime(dtpAdvanceDate.EditValue.ToString());
            }
            mSelectedAdvanceAmount.Status = "AMS01";
            mSelectedAdvanceAmount.Amount = CommonFunctions.ToDecimal(txtAdvanceAmount.Text);
            mSelectedAdvanceAmount.Remark = txtRemark.Text;

            if (ThisActionType == ActionType.New)
            {
                mSelectedAdvanceAmount.AdvanceType = "ADT01";
                mSelectedAdvanceAmount.CreatedDate = DateTime.Now;
                mSelectedAdvanceAmount.CreatedBy = CommonParameter.CurrentUser.PersonId;
            }
            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                mSelectedAdvanceAmount.LastModifiedDate = DateTime.Now;
                mSelectedAdvanceAmount.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            }
        }
        private bool CheckData()
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpAdvanceDate.EditValue == null)
            {
                MessageBox.Show("预支日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtAdvanceAmount.Text))
            {
                MessageBox.Show("预支金额不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (ThisApproveType == ApproveType.FIN && ThisActionType == ActionType.Approve)
            {
                bool selected = false;
                foreach (CheckedListBoxItem node in treePaymentType.Items)
                {
                    if (node.CheckState==CheckState.Checked)
                    {
                        selected = true;
                        break;
                    }
                }
                if (selected == false)
                {
                    MessageBox.Show("支付方式不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                if (ThisActionType == ActionType.New)
                {
                    SetEntity();
                    bool isSuccessful = FinAdvanceAmountBs.InsertFinAdvanceAmount(mSelectedAdvanceAmount);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    SetEntity();
                    bool isSuccessful = FinAdvanceAmountBs.UpdateFinAdvanceAmount(mSelectedAdvanceAmount);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Approve)
                {
                    mSelectedAdvanceAmount.ApprovedDate = DateTime.Now;
                    mSelectedAdvanceAmount.ApprovedBy = CommonParameter.CurrentUser.PersonId;
                    bool isSuccessful = FinAdvanceAmountBs.ApproveAdvanceAmount(mSelectedAdvanceAmount);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("审核失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            SetPersonFields();
            SetFields();           
            if (ThisActionType == ActionType.Approve && ThisApproveType == ApproveType.FIN)
            {
                btnReject.Visible = false;
                btnApprove.Text = "支付";
                treePaymentType.Enabled = true;
            }
            else
            {
                treePaymentType.Enabled = false;
            }
        }
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        protected override void Click_Approve()
        {
            if (mSelectedAdvanceAmount == null)
            {
                return;
            }
            if (ThisActionType == ActionType.Approve)
            {
                switch (ThisApproveType)
                {
                    case ApproveType.CEO:
                        mSelectedAdvanceAmount.Status = "AMS02";
                        break;
                    case ApproveType.FIN:
                        mSelectedAdvanceAmount.Status = "AMS04";
                        mSelectedAdvanceAmount.PaymentType = "";

                        foreach (CheckedListBoxItem node in treePaymentType.Items)
                        {
                            if (node.CheckState==CheckState.Checked)
                            {
                                if (string.IsNullOrEmpty(mSelectedAdvanceAmount.PaymentType))
                                {
                                    mSelectedAdvanceAmount.PaymentType = node.Value.ToString();
                                }
                                else
                                {
                                    mSelectedAdvanceAmount.PaymentType += "," + node.Value.ToString();
                                }
                            }
                        }
                        break;
                }
            }
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        protected override void Click_Reject()
        {
            if (mSelectedAdvanceAmount == null)
            {
                return;
            }
            mSelectedAdvanceAmount.Status = "AMS03";
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                mSelectedPerson = frm.SelectedPerson;
                SetPersonFields();
            }
            else
            {
                mSelectedPerson = null;
                SetPersonFields();
            }
        }
        #endregion

    }
}
